package com.example.labtest.webapi;

import com.example.labtest.core.PageUtils;
import com.example.labtest.domain.StudentGrade;
import com.example.labtest.domain.Student_info;
import com.example.labtest.helper.R;
import com.example.labtest.helper.RUtil;
import com.example.labtest.service.StudentGradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;



@RestController
@RequestMapping("/webapi/studentGrade")
public class StudentGradeRestController {

    @Autowired
    private StudentGradeService studentGradeService;

    /**
     * 读取的url：/webapi/studentGrade/managegrade
     *
     * @return
     */
    @GetMapping("/managegrade")
    public List<StudentGrade> getAll() {
        List<StudentGrade> studentGrades = studentGradeService.findAll();
        return studentGrades;
    }

    /**
     * 分页,读取全部内容
     * 用如下两条语句传参数
     *
     * @return
     * @RequestParam(value="page",defaultValue = "0")Integer page,
     * @RequestParam(value="size",defaultValue = "10")Integer size
     */
    @GetMapping("/getbypage")
    public R<StudentGrade> getByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                     @RequestParam(value = "size", defaultValue = "10") Integer size,
                                     @RequestParam(value = "sname", defaultValue = "") String sname
    ) {
        //按id排倒序
        Sort sort = Sort.by(Sort.Direction.DESC, "id");

        Page<StudentGrade> gradePage;
        if (StringUtils.isEmpty(sname)) {
            Pageable pageable = PageRequest.of(page, size, sort);
            gradePage = studentGradeService.findAll(pageable);
        } else {
            StudentGrade studentGrade = new StudentGrade();
            studentGrade.setSname(sname);

            Pageable pageable = (Pageable) PageRequest.of(0, 10, sort);
            //模糊查询,使用ExampleMatcher来匹配名字模糊查询，全部模糊查询 %{name}%
            ExampleMatcher matcher = ExampleMatcher.matching()
                    .withMatcher("sname", ExampleMatcher.GenericPropertyMatchers.contains());

            Example<StudentGrade> example = Example.of(studentGrade, matcher);

            gradePage = studentGradeService.findAll(example, pageable);
        }

        PageUtils pageUtils = new PageUtils(gradePage.getContent(), gradePage.getTotalElements());
        return RUtil.success(pageUtils);
    }

    @GetMapping("/getbyCname")
    public PageUtils getByCname(@RequestParam(value = "page", defaultValue = "0") Integer page,

                              @RequestParam(value = "size", defaultValue = "10") Integer size,
                              @RequestParam(value = "cname", defaultValue = "") String cname
    ) {
        //按id排倒序
        Sort sort = Sort.by(Sort.Direction.DESC, "id");


        Page<StudentGrade> gradePage;
        if (StringUtils.isEmpty(cname)) {
            Pageable pageable = PageRequest.of(page, size, sort);
            gradePage = studentGradeService.findAll(pageable);
        } else {
            StudentGrade studentGrade = new StudentGrade();
            studentGrade.setCname(cname);

            Pageable pageable = (Pageable) PageRequest.of(0, 10, sort);
            //模糊查询,使用ExampleMatcher来匹配名字模糊查询，全部模糊查询 %{name}%
            ExampleMatcher matcher = ExampleMatcher.matching()
                    .withMatcher("cname", ExampleMatcher.GenericPropertyMatchers.contains());

            Example<StudentGrade> example = Example.of(studentGrade, matcher);

            gradePage = studentGradeService.findAll(example, pageable);
        }

        PageUtils pageUtils = new PageUtils(gradePage.getContent(), gradePage.getTotalElements());

        return pageUtils;

    }

    @GetMapping("/getbySclass")
    public PageUtils getBySclass(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                 @RequestParam(value = "size", defaultValue = "10") Integer size,
                                 @RequestParam(value = "sclass", defaultValue = "") String sclass
    ) {
        //按id排倒序
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Page<StudentGrade> studentGradePage;
        if (StringUtils.isEmpty(sclass)) {
            Pageable pageable = PageRequest.of(page, size, sort);
            studentGradePage = studentGradeService.findAll(pageable);
        } else {

            Pageable pageable = PageRequest.of(page, size, sort);
            studentGradePage = studentGradeService.findByNameLike("%" + sclass + "%", pageable);

        }

        PageUtils pageUtils = new PageUtils(studentGradePage.getContent(), studentGradePage.getTotalElements());

        return pageUtils;

    }







    /**
     * 新增学生的方法./webapi/studentInfo/insert(对应的url)
     *
     * @param studentGrade
     * @return
     */
    //新增是一个post操作，所以需要postMapping
    @PostMapping("/insert")
    public StudentGrade insert(StudentGrade studentGrade) {

        StudentGrade studentGrade1 = studentGradeService.insert(studentGrade);

        return studentGrade1;
    }

    /**
     * 读取的url：/webapi/studentInfo/get/id
     *
     * @return
     */
    @GetMapping("/get/{id}")
    public StudentGrade get(@PathVariable Long id) {
        StudentGrade studentGrade = studentGradeService.getById(id);
        studentGrade.getScore();
        return studentGrade;

    }

    /**
     * 读取的url：/webapi/studentInfo/findByName
     *
     * @return
     */

    @GetMapping("/findByName")
    public R<StudentGrade> findByName(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                      @RequestParam(value = "size", defaultValue = "10") Integer size,
                                      @RequestParam(value = "sname", defaultValue = "") String sname) {
        //按id排倒序
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Page<StudentGrade> gradePage;
//        Page<Student>all;
//        Pageable pageable=PageRequest.of(offset/limit,limit,Sort.by("id").descending());
        if (sname == null || sname.equals("")) {
            Pageable pageable = PageRequest.of(page, size, sort);
            gradePage = studentGradeService.findAll(pageable);
        } else {
            Pageable pageable = PageRequest.of(page, size, sort);
            gradePage = studentGradeService.findByNameLike("%" + sname + "%", pageable);
        }
        // List<Student> students = studentService.findByName(name);
        PageUtils pageUtils = new PageUtils(gradePage.getContent(), gradePage.getTotalElements());
        return RUtil.success(pageUtils);
    }


    /**
     * 更新学生的方法./webapi/studentInfo/update(对应的url)
     *
     * @param studentGrade
     * @return
     */
    //新增是一个put操作，所以需要postMapping
    @PutMapping("/update")
    public StudentGrade update(StudentGrade studentGrade) {


        StudentGrade oldstudent = studentGradeService.getById(studentGrade.getId());
        if (StringUtils.isEmpty(studentGrade.getScore())) {
            //没有成绩，那么就用旧成绩
            studentGrade.setScore(oldstudent.getScore());
        }

        studentGrade = studentGradeService.update(studentGrade);

        return studentGrade;
    }

    /**
     * 删除学生的方法./webapi/studentInfo/delete(对应的url)
     *
     * @param id
     */
    //修改成用路径的方式来删除
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Long id) {
        studentGradeService.delete(id);
    }
}
